<template>
    <BasicLayout>
        <template #wrapper>
            <el-card class="box-card">
                <el-form
                    ref="queryForm"
                    :model="queryParams"
                    :inline="true"
                    label-width="68px"
                >
                    <el-form-item label="班级名称" prop="title">
                        <el-input
                            v-model="queryParams.title"
                            placeholder="班级名称"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>

                    <el-form-item label="简介" prop="about">
                        <el-input
                            v-model="queryParams.about"
                            placeholder="请输入简介"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>
                    <el-form-item label="班主任" prop="headTeacherId">
                        <el-input
                            v-model="queryParams.headTeacherId"
                            placeholder="请输入班主任"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>

                    <el-form-item label="开放购买" prop="buyable">
                        <el-select
                            v-model="queryParams.buyable"
                            placeholder="班级是否开放购买"
                            clearable
                            size="small"
                        >
                            <el-option
                                v-for="dict in buyableOptions"
                                :key="dict.dictValue"
                                :label="dict.dictLabel"
                                :value="dict.dictValue"
                            />
                        </el-select>
                    </el-form-item>

                    <el-form-item label="是否封闭" prop="private">
                        <el-select
                            v-model="queryParams.private"
                            placeholder="班级是否封闭"
                            clearable
                            size="small"
                        >
                            <el-option
                                v-for="dict in privateOptions"
                                :key="dict.dictValue"
                                :label="dict.dictLabel"
                                :value="dict.dictValue"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否展示" prop="showable">
                        <el-select
                            v-model="queryParams.showable"
                            placeholder="班级是否开放展示"
                            clearable
                            size="small"
                        >
                            <el-option
                                v-for="dict in showableOptions"
                                :key="dict.dictValue"
                                :label="dict.dictLabel"
                                :value="dict.dictValue"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="班级状态" prop="status">
                        <el-select
                            v-model="queryParams.status"
                            placeholder="班级状态"
                            clearable
                            size="small"
                        >
                            <el-option
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictLabel"
                                :value="dict.dictValue"
                            />
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            size="mini"
                            @click="handleQuery"
                            >搜索</el-button
                        >
                        <el-button
                            icon="el-icon-refresh"
                            size="mini"
                            @click="resetQuery"
                            >重置</el-button
                        >
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button
                            v-permisaction="['edu:classroom:add']"
                            type="primary"
                            icon="el-icon-plus"
                            size="mini"
                            @click="handleAdd"
                            v-if="schoolId"
                            >新增
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            v-permisaction="['edu:classroom:edit']"
                            type="success"
                            icon="el-icon-edit"
                            size="mini"
                            :disabled="single"
                            @click="handleUpdate"
                            >修改
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            v-permisaction="['edu:classroom:remove']"
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            :disabled="multiple"
                            @click="handleDelete"
                            >删除
                        </el-button>
                    </el-col>
                </el-row>

                <el-table
                    v-loading="loading"
                    :data="educlassroomList"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column
                        type="selection"
                        width="55"
                        align="center"
                    />
                    <el-table-column
                        label="班级头像"
                        align="center"
                        prop="cover"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <el-avatar
                                :size="50"
                                :src="scope.row.cover"
                            ></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="标题"
                        align="center"
                        prop="title"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="简介"
                        align="center"
                        prop="about"
                        :show-overflow-tooltip="true"
                    /><el-table-column
                        label="班主任"
                        align="center"
                        prop="headTeacherId"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="点击数"
                        align="center"
                        prop="hit"
                        :show-overflow-tooltip="true"
                    /><el-table-column
                        label="课时数"
                        align="center"
                        prop="lessonNum"
                        :show-overflow-tooltip="true"
                    /><el-table-column
                        label="班级笔记数量"
                        align="center"
                        prop="noteNum"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="学员数"
                        align="center"
                        prop="studentNum"
                        :show-overflow-tooltip="true"
                    /><el-table-column
                        label="价格"
                        align="center"
                        prop="price"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="vip价格"
                        align="center"
                        prop="vipPrice"
                        :show-overflow-tooltip="true"
                    />

                    <el-table-column
                        label="是否开放购买"
                        align="center"
                        prop="buyable"
                        :formatter="buyableFormat"
                        width="100"
                    >
                        <template slot-scope="scope">
                            {{ buyableFormat(scope.row) }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="是否封闭班级"
                        align="center"
                        prop="private"
                        :formatter="privateFormat"
                        width="100"
                    >
                        <template slot-scope="scope">
                            {{ privateFormat(scope.row) }}
                        </template> </el-table-column
                    ><el-table-column
                        label="是否开放展示"
                        align="center"
                        prop="showable"
                        :formatter="showableFormat"
                        width="100"
                    >
                        <template slot-scope="scope">
                            {{ showableFormat(scope.row) }}
                        </template> </el-table-column
                    ><el-table-column
                        label="班级状态"
                        align="center"
                        prop="status"
                        :formatter="statusFormat"
                        width="100"
                    >
                        <template slot-scope="scope">
                            {{ statusFormat(scope.row) }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        fixed="right"
                        width="120"
                        align="center"
                        class-name="small-padding fixed-width"
                    >
                        <template slot-scope="scope">
                            <el-button
                                v-permisaction="['edu:classroom:edit']"
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                                >修改
                            </el-button>
                            <el-button
                                v-permisaction="['edu:classroom:remove']"
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.row)"
                                >删除
                            </el-button>
                            <el-dropdown
                                @command="handleCommand($event, scope.row)"
                            >
                                <span class="el-dropdown-link">
                                    更多<i
                                        class="el-icon-arrow-down el-icon--right"
                                    ></i>
                                </span>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="course"
                                        >课程管理</el-dropdown-item
                                    >
                                    <el-dropdown-item command="teacher"
                                        >教师管理</el-dropdown-item
                                    >
                                    <el-dropdown-item command="classmate"
                                        >学员管理</el-dropdown-item
                                    >
                                </el-dropdown-menu>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination
                    v-show="total > 0"
                    :total="total"
                    :page.sync="queryParams.pageIndex"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
                />

                <!-- 添加或修改对话框 -->
                <el-dialog :title="title" :visible.sync="open" width="800px">
                    <el-form
                        ref="form"
                        :model="form"
                        :rules="rules"
                        label-width="80px"
                    >
                        <el-tabs type="border-card">
                            <el-tab-pane label="基础设置">
                                <el-form-item label="班级头像" prop="cover">
                                    <uploadImg
                                        :imgUrl.sync="form.cover"
                                    ></uploadImg>
                                </el-form-item>
                                <el-form-item label="标题" prop="title">
                                    <el-input
                                        v-model="form.title"
                                        placeholder="标题"
                                        :style="{ width: '220px' }"
                                    />
                                </el-form-item>
                                <el-form-item
                                    label="班主任"
                                    prop="headTeacherId"
                                >
                                    <SelectTeacher
                                        :teacherIds="form.headTeacherId"
                                        :multiple="false"
                                        v-on:setTeacherIds="setTeacherIds"
                                    />
                                </el-form-item>
                                <el-form-item label="简介" prop="about">
                                    <el-input
                                        type="textarea"
                                        :rows="5"
                                        placeholder="请输入简介"
                                        v-model="form.about"
                                    ></el-input>
                                </el-form-item>

                                <el-form-item label="开放展示" prop="showable">
                                    <el-switch
                                        v-model="form.showable"
                                        active-value="T"
                                        inactive-value="F"
                                    ></el-switch>
                                </el-form-item>
                                <el-form-item label="班级状态" prop="status">
                                    <el-radio-group v-model="form.status">
                                        <el-radio
                                            v-for="dict in statusOptions"
                                            :key="dict.dictValue"
                                            :label="dict.dictValue"
                                            >{{ dict.dictLabel }}</el-radio
                                        >
                                    </el-radio-group>
                                </el-form-item>

                                <el-form-item label="权重" prop="weigh">
                                    <el-input-number
                                        v-model="form.weigh"
                                        placeholder="权重"
                                    ></el-input-number>
                                </el-form-item>
                            </el-tab-pane>

                            <el-tab-pane label="班级详情">
                                <el-form-item label="课程数" prop="hit">
                                    <el-input-number
                                        v-model="form.courseNum"
                                        placeholder="课程数"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item label="点击数" prop="hit">
                                    <el-input-number
                                        v-model="form.hit"
                                        placeholder="点击数"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item label="课时数" prop="hit">
                                    <el-input-number
                                        v-model="form.lessonNum"
                                        placeholder="课时数"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item label="笔记数" prop="hit">
                                    <el-input-number
                                        v-model="form.noteNum"
                                        placeholder="笔记数"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item label="学员数" prop="hit">
                                    <el-input-number
                                        v-model="form.studentNum"
                                        placeholder="学员数"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item
                                    label="班级详情"
                                    prop="description"
                                >
                                    <tinymce
                                        v-model="form.description"
                                        :height="300"
                                        ref="tinymce"
                                    />
                                </el-form-item>
                            </el-tab-pane>
                            <el-tab-pane label="加入班级设置">
                                <el-form-item label="购买加入" prop="buyable">
                                    <el-switch
                                        v-model="form.buyable"
                                        active-value="T"
                                        inactive-value="F"
                                    ></el-switch>
                                </el-form-item>

                                <el-form-item
                                    label="原价"
                                    prop="price"
                                    v-if="form.buyable == 'T'"
                                >
                                    <el-input-number
                                        v-model="form.price"
                                        :precision="2"
                                        :step="0.1"
                                        :min="0"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item
                                    label="vip价"
                                    prop="vipPrice"
                                    v-if="form.buyable == 'T'"
                                >
                                    <el-input-number
                                        v-model="form.vipPrice"
                                        :precision="2"
                                        :step="0.1"
                                        :min="0"
                                    ></el-input-number>
                                </el-form-item>

                                <el-form-item label="邀请码加入" prop="private">
                                    <el-switch
                                        v-model="form.private"
                                        active-value="T"
                                        inactive-value="F"
                                    ></el-switch>
                                </el-form-item>

                                <el-form-item
                                    label="邀请码"
                                    prop="invitationCode"
                                    v-if="form.private == 'T'"
                                >
                                    <el-input
                                        v-model="form.invitationCode"
                                        placeholder="邀请码"
                                        :style="{ width: '120px' }"
                                    />
                                </el-form-item>
                            </el-tab-pane>
                        </el-tabs>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="submitForm"
                            >确 定</el-button
                        >
                        <el-button @click="cancel">取 消</el-button>
                    </div>
                </el-dialog>
            </el-card>
            <!-- 学员管理 -->
            <classmate
                :courseId="classroomId"
                paramType="classroomInfo"
                :drawer.sync="drawer"
            ></classmate>
            <!-- 学员管理 -->
            <teacher
                :courseId="classroomId"
                paramType="classroomInfo"
                :drawer.sync="drawer"
            ></teacher>
        </template>
    </BasicLayout>
</template>

<style>
.el-dropdown-link {
    cursor: pointer;
    color: #409eff;
    font-size: 12px;
    margin-left: 10px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
<script>
import {
    addEduClassroom,
    delEduClassroom,
    getEduClassroom,
    listEduClassroom,
    updateEduClassroom
} from "@/api/edu/classroom";
//调用编辑器
import Tinymce from "@/components/Tinymce";

//老师选择器
import SelectTeacher from "@/views/edu/teacher/components/SelectTeacher";
// 学员管理组件
import classmate from "@/views/edu/components/classmate";
// 老师管理
import teacher from "@/views/edu/components/teacher";
import { getToken } from "@/utils/auth";
export default {
    name: "Config",
    components: {
        //使用编辑器
        Tinymce,
        SelectTeacher,
        classmate,
        teacher
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            isEdit: false,
            // 类型数据字典
            typeOptions: [],
            educlassroomList: [],
            buyableOptions: [],
            privateOptions: [],
            showableOptions: [],
            statusOptions: [],
            // 查询参数
            queryParams: {
                pageIndex: 1,
                pageSize: 10,
                about: undefined,
                buyable: undefined,
                courseNum: undefined,
                headTeacherId: undefined,
                private: undefined,
                showable: undefined,
                status: undefined,
                title: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                about: [
                    { required: true, message: "简介不能为空", trigger: "blur" }
                ],
                buyable: [
                    {
                        required: true,
                        message: "是否开放购买不能为空",
                        trigger: "blur"
                    }
                ],
                courseNum: [
                    {
                        required: true,
                        message: "课程数不能为空",
                        trigger: "blur"
                    }
                ],
                headTeacherId: [
                    {
                        required: true,
                        message: "班主任ID不能为空",
                        trigger: "blur"
                    }
                ],
                private: [
                    {
                        required: true,
                        message: "是否封闭班级不能为空",
                        trigger: "blur"
                    }
                ],
                showable: [
                    {
                        required: true,
                        message: "是否开放展示不能为空",
                        trigger: "blur"
                    }
                ],
                status: [
                    {
                        required: true,
                        message: "班级状态不能为空",
                        trigger: "blur"
                    }
                ],
                title: [
                    { required: true, message: "标题不能为空", trigger: "blur" }
                ]
            },
            //上传
            uploadImageAction:
                process.env.VUE_APP_BASE_API + "/api/v1/common/uploadImage",
            myHeader: {
                Authorization: "Bearer " + getToken()
            },
            drawer: "",
            classroomId: "",
            schoolId: ""
        };
    },
    created() {
        this.$store.dispatch("tagsView/updateVisitedView", this.$route);

        this.schoolId = this.$route.params.schoolId;
        this.getList();
        this.getDicts("sys_bool").then(response => {
            this.buyableOptions = response.data;
        });

        this.getDicts("sys_bool").then(response => {
            this.privateOptions = response.data;
        });

        this.getDicts("sys_bool").then(response => {
            this.showableOptions = response.data;
        });

        this.getDicts("edu_status").then(response => {
            this.statusOptions = response.data;
        });
    },
    methods: {
        // 按钮操作
        handleCommand(command, data) {
            this.classroomId = data.classroomId;
            if (command == "course") {
                // this.handleUpdate(data);
                this.$router.push({
                    name: "teacherCourse",
                    params: {
                        name: data.title + "的课程",
                        classroomId: data.classroomId,
                        schoolId: data.schoolId
                    }
                });
            } else {
                this.drawer = command;
            }
        },
        //接收用户IDs
        setTeacherIds: function(teacherIds) {
            // childValue就是子组件传过来的值
            // console.log("userIds: "+userIds);
            console.log(teacherIds);
            if (teacherIds) {
                this.form.headTeacherId = teacherIds;
            }
        },
        /** 查询参数列表 */
        getList() {
            this.loading = true;
            listEduClassroom(
                this.addDateRange(this.queryParams, this.dateRange),
                this.schoolId
            ).then(response => {
                this.educlassroomList = response.data.list;
                this.total = response.data.count;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                about: undefined,
                buyable: undefined,
                classroomId: undefined,
                courseNum: undefined,
                cover: undefined,
                description: undefined,
                headTeacherId: undefined,
                hit: undefined,
                lessonNum: undefined,
                noteNum: undefined,
                price: undefined,
                private: undefined,
                showable: undefined,
                status: undefined,
                studentNum: undefined,
                title: undefined,
                vipPrice: undefined,
                weigh: undefined
            };
            this.resetForm("form");
        },
        buyableFormat(row) {
            return this.selectDictLabel(this.buyableOptions, row.buyable);
        },
        privateFormat(row) {
            return this.selectDictLabel(this.privateOptions, row.private);
        },
        showableFormat(row) {
            return this.selectDictLabel(this.showableOptions, row.showable);
        },
        statusFormat(row) {
            return this.selectDictLabel(this.statusOptions, row.status);
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageIndex = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加班级";
            this.isEdit = false;
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.classroomId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const classroomId = row.classroomId || this.ids;
            getEduClassroom(classroomId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改班级";
                this.isEdit = true;
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.$route.params.schoolId) {
                        this.form.schoolId = this.$route.params.schoolId;
                    }
                    if (this.form.classroomId !== undefined) {
                        updateEduClassroom(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addEduClassroom(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const Ids = row.classroomId || this.ids;
            this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(function() {
                    return delEduClassroom(Ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function() {});
        },

        //图片上传 start

        handleRemove(file, fileList) {
            console.log(file, fileList);
            this.form.cover = "";
            console.log(this.form.cover);
        },

        //上传组图成功
        handlepostingImageSuccess(res, file) {
            console.log(res.data);
            if (res.code == "200") {
                this.form.cover = res.data.url;
            }

            console.log(this.form.avatar);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        }

        //图片上传 end
    }
};
</script>
